package com.zy.explore.wechat.assembler;

import com.zy.explore.wechat.frm.ExampleFrm;
import com.zy.explore.wechat.frm.SortFieldFrm;
import com.zy.explore.wechat.vo.ExampleVO;
import com.zy.explore.wechat.domain.jpa.entity.JpaExampleEntity;
import com.zy.explore.wechat.util.DataBeanUtil;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * description Jpa示例Assembler
 *
 * @author ZY
 * @date 2021年05月15日 14:22
 */

public class JpaExampleAssembler {

    /**
     * 私有化构造器
     */
    private JpaExampleAssembler() {
    }

    /**
     * 实体类转Frm
     *
     * @param entity 实体类
     * @return 转换后的对象
     */
    public static ExampleFrm covertToFrm(JpaExampleEntity entity) {
        ExampleFrm exampleFrm = DataBeanUtil.copyOf(entity, ExampleFrm.class, true);
        if (entity.getId() != null) {
            exampleFrm.setId(entity.getId() + "");
        }
        return exampleFrm;
    }

    /**
     * 实体类转VO
     *
     * @param entity 实体类
     * @return 转换后的对象
     */
    public static ExampleVO convertToVO(JpaExampleEntity entity) {
        ExampleVO exampleVO = DataBeanUtil.copyOf(entity, ExampleVO.class, true);
        if (entity.getId() != null) {
            exampleVO.setId(entity.getId() + "");
        }
        return exampleVO;
    }

    /**
     * Frm转实体类
     *
     * @param frm 参数对象
     * @return 转换后的实体类对象
     */
    public static JpaExampleEntity convertFromFrm(ExampleFrm frm) {
        JpaExampleEntity jpaExampleEntity = DataBeanUtil.copyOf(frm, JpaExampleEntity.class, true);
        if (!StringUtils.isEmpty(frm.getId())) {
            jpaExampleEntity.setId(Integer.parseInt(frm.getId()));
        }
        return jpaExampleEntity;
    }

    /**
     * VO转实体类
     *
     * @param vo 参数对象
     * @return 转换后的实体类对象
     */
    public static JpaExampleEntity convertFromVO(ExampleVO vo) {
        JpaExampleEntity jpaExampleEntity = DataBeanUtil.copyOf(vo, JpaExampleEntity.class, true);
        if (!StringUtils.isEmpty(vo.getId())) {
            jpaExampleEntity.setId(Integer.parseInt(vo.getId()));
        }
        return jpaExampleEntity;
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<JpaExampleEntity> convertToSpecification(ExampleFrm frm) {
        if (frm.getPageNum() == null) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (!StringUtils.isEmpty(frm.getId())) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), Integer.parseInt(frm.getId())));
            }
            if (!StringUtils.isEmpty(frm.getName())) {
                predicateList.add(criteriaBuilder.like(root.get("name"), "%" + frm.getName() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getAddress())) {
                predicateList.add(criteriaBuilder.like(root.get("address"), "%" + frm.getAddress() + "%"));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

    /**
     * Frm转Sort排序条件
     *
     * @param frm 排序参数对象
     * @return 返回JPA所需的排序对象
     */
    public static Sort getSort(ExampleFrm frm) {
        List<SortFieldFrm> sortFields = frm.getSortFields();
        List<Sort.Order> orderList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sortFields)) {
            for (SortFieldFrm sortFrm : sortFields) {
                if (StringUtils.isEmpty(sortFrm.getOrder())) {
                    continue;
                }
                orderList.add(new Sort.Order(Objects.equals(sortFrm.getOrder().toUpperCase(), "ASC") ? Sort.Direction.ASC : Sort.Direction.DESC, sortFrm.getPropName()));
            }
        }
        return Sort.by(orderList);
    }

}
